package cn.cjlmonster.hyouka_assistant.webView;

import android.content.Context;
import android.content.MutableContextWrapper;
import android.util.Log;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * WebView缓存池
 *
 * @author cjl
 */
public class WebViewPool {

    private static final String TAG = "WebViewPool";

    // 存放空闲的webView（空闲池）
    private List<RichWebView> mIdleWebViewList;
    // 存放当前正在使用的webView（活动池）
    private List<RichWebView> mUsingWebViewList;

    private static class SingleHolder {
        private static final WebViewPool instance = new WebViewPool();
    }

    public static WebViewPool getInstance() {
        return SingleHolder.instance;
    }

    private WebViewPool() {
    }

    private Context context;

    /**
     * 初始化，一般在application里
     */
    public void init(Context context) {
        Log.i(TAG, "init: 初始化");
        this.context = context;
        mIdleWebViewList = new CopyOnWriteArrayList<>();
        mUsingWebViewList = new ArrayList<>();
        MutableContextWrapper contextWrapper = new MutableContextWrapper(context);
        RichWebView webView = new RichWebView(contextWrapper);
        mIdleWebViewList.add(webView);
    }

    public void deInit() {
        Log.i(TAG, "deInit: 释放资源");
        context = null;
        if (mIdleWebViewList != null) {
            for (RichWebView webView : mIdleWebViewList) {
                webView.clearHistory();
                webView.destroy();
            }
            mIdleWebViewList.clear();
        }
        if (mUsingWebViewList != null) {
            for (RichWebView webView : mUsingWebViewList) {
                webView.clearHistory();
                webView.destroy();
            }
            mUsingWebViewList.clear();
        }
    }

    /**
     * 从缓存池里获取一个webView对象
     *
     * @param context 上下文
     * @return webView
     */
    public RichWebView acquireWebView(Context context) {
        if (mIdleWebViewList != null && mIdleWebViewList.size() > 0) {
            RichWebView webView = mIdleWebViewList.remove(0);
            MutableContextWrapper contextWrapper = (MutableContextWrapper) webView.getContext();
            contextWrapper.setBaseContext(context);
            mUsingWebViewList.add(webView);
            return webView;
        } else {
            MutableContextWrapper contextWrapper = new MutableContextWrapper(context);
            RichWebView webView = new RichWebView(contextWrapper);
            mUsingWebViewList.add(webView);
            return webView;
        }
    }

    /**
     * webView用完，在界面销毁前放回缓存池，以便下次使用
     *
     * @param webView 需要放回缓存池的webView
     */
    public void recycleWebView(RichWebView webView) {
        if (webView == null) return;
        ViewGroup viewParent = (ViewGroup) webView.getParent();
        if (viewParent != null) {
            viewParent.removeView(webView);
        }
        webView.loadUrl("about:blank");
        if (mUsingWebViewList != null && mUsingWebViewList.size() > 0) {
            mUsingWebViewList.remove(webView);
            MutableContextWrapper contextWrapper = (MutableContextWrapper) webView.getContext();
            contextWrapper.setBaseContext(context);
            webView.setWebViewClient(null);
            webView.setWebChromeClient(null);
            mIdleWebViewList.add(webView);
        } else {
            webView.clearHistory();
            webView.destroy();
        }
    }
}
